package com.jiuyin.dianjing.ui.activity.team;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ImageView;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.jiuyin.dianjing.adapter.SmartViewHolder;
import com.jiuyin.dianjing.model.TeamMember;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.util.DisplayUtil;
import com.jiuyin.dianjing.util.ToastUtil;

import java.util.ArrayList;
import java.util.Iterator;

import butterknife.BindView;

import static androidx.recyclerview.widget.DividerItemDecoration.VERTICAL;

/**
 * 真正设置管理界面
 */
public class ManagerSet2Activity extends BaseActivity {
    public static final int TYPE_REMOVE = 1;
    public static final int TYPE_ADD = 0;
    int mType;
    @BindView(R.id.rv_members)
    RecyclerView rvMembers;
    @BindView(R.id.bt_submit)
    View btSubmit;
    private ArrayList<TeamMember> members;
    private ArrayList<TeamMember> selected = new ArrayList<>();

    @Override
    protected int setLayoutId() {
        return R.layout.activity_manager_set2;
    }

    @Override
    protected void initData() {
        mType = getIntent().getIntExtra("type", TYPE_REMOVE);
        members = getIntent().getParcelableArrayListExtra("members");
    }

    @Override
    protected void initView() {
        setTitle("设置管理员");
        if (mType == TYPE_REMOVE) {
            btSubmit.setVisibility(View.GONE);
            setRightButton(R.drawable.shanchuguanli, DisplayUtil.dp2px(getApplicationContext(), 48), DisplayUtil.dp2px(getApplicationContext(), 26), v -> {
                if (selected.size() > 0) {
                    new AlertDialog.Builder(this)
                            .setMessage("确定删除？")
                            .setNegativeButton("取消", null)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent data = new Intent(ManagerSet2Activity.this, ManagerSetActivity.class);
                                    data.putParcelableArrayListExtra("selected", selected);
                                    setResult(RESULT_OK, data);
                                    finish();
                                }
                            })
                            .show();
                }
            });
        } else if (mType == TYPE_ADD) {
            setRightButtonVisible(false);
            btSubmit.setOnClickListener(v -> {
                Intent data = new Intent(this, ManagerSetActivity.class);
                data.putParcelableArrayListExtra("selected", selected);
                setResult(RESULT_OK, data);
                finish();
            });

            Iterator<TeamMember> it_b = members.iterator();
            while (it_b.hasNext()) {
                TeamMember it = it_b.next();
                if (it.part == 1) {
                    it_b.remove();
                    selected.add(it);
                }
            }
        }
        BaseRecyclerAdapter<TeamMember> adapter = new BaseRecyclerAdapter<TeamMember>(members, R.layout.item_member_multiple) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, TeamMember model, int position) {
                ImageView ivHead = holder.itemView.findViewById(R.id.iv_head);
                CheckedTextView tvName = holder.itemView.findViewById(R.id.tv_name);
                Glide.with(ManagerSet2Activity.this)
                        .load(model.header)
                        .override(DisplayUtil.dp2px(ManagerSet2Activity.this, 26.7f))
                        .into(ivHead);
                switch (model.part) {
                    case 0:
                        tvName.setEnabled(false);
                        break;
                    case 1:
                        tvName.setEnabled(true);
                        tvName.setChecked(true);
                        break;
                    default:
                        tvName.setEnabled(true);
                        tvName.setChecked(false);
                        break;
                }
                if (selected.contains(model)) {
                    if (model.part != 1) {
                        selected.remove(model);
                    }
                } else {
                    if (model.part == 1) {
                        selected.add(model);
                    }
                }
                tvName.setOnClickListener(v -> {
                    if (model.part == 1) {
                        model.part = 2;
                        notifyDataSetChanged();
                    } else {
                        if (selected.size() >= 3) {
                            ToastUtil.showShort("最多只能添加三个战队管理");
                            return;
                        }
                        model.part = 1;
                        notifyDataSetChanged();
                    }
                });
            }
        };
        rvMembers.addItemDecoration(new DividerItemDecoration(this, VERTICAL));
        rvMembers.setAdapter(adapter);
    }
}
